package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.domain.Market;
import com.gaogzhen.dto.CoinDto;
import com.gaogzhen.dto.MarketDto;
import com.gaogzhen.feign.CoinFeign;
import com.gaogzhen.mapper.MarketMapper;
import com.gaogzhen.mappers.MarketDtoMapper;
import com.gaogzhen.model.RRException;
import com.gaogzhen.service.MarketService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Administrator
* @date 2022-12-08 10:18
*/
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService{

    @Autowired
    private CoinFeign   coinFeign;


    @Override
    public Page<Market> findByPage(Page<Market> page, Long tradeAreaId, Byte status) {
        return page(page, new LambdaQueryWrapper<Market>()
                            .eq(tradeAreaId != null, Market::getTradeAreaId, tradeAreaId)
                            .eq(status != null, Market::getStatus, status)
        );
    }

    @Override
    public void updateStatus(Long id, Byte status) {
        boolean update = update(new LambdaUpdateWrapper<Market>()
                .eq(Market::getId, id)
                .set(Market::getStatus, status)
        );
        if (!update) {
            throw new RRException("更新状态失败");
        }
    }

    @Override
    public Map<Long, List<Market>> getMarketsByTradeAreaId(List<Long> ids) {
        Map<Long, List<Market>> map = new HashMap<>();
        for (Long id : ids) {
            List<Market> markets = list(new LambdaQueryWrapper<Market>()
                                            .eq(Market::getTradeAreaId, id)
                                            .eq(Market::getStatus, 1)
                                            .orderByAsc(Market::getSort)

            );
            map.put(id, markets);
        }
        return map;
    }

    @Override
    public Market getBySymbol(String symbol) {
        return getOne(new LambdaQueryWrapper<Market>().eq(Market::getSymbol, symbol).eq(Market::getStatus, 1));
    }

    @Override
    public MarketDto findByCoinId(Long buyCoinId, Long sellCoinId) {
        LambdaQueryWrapper<Market> eq = new LambdaQueryWrapper<Market>()
                .eq(Market::getBuyCoinId, buyCoinId)
                .eq(Market::getSellCoinId, sellCoinId)
                .eq(Market::getStatus, 1);
        Market one = getOne(eq);
        if (one == null) {
            return null;
        }
        MarketDto marketDto = MarketDtoMapper.INSTANCE.convert2Dto(one);
        return marketDto;
    }


    @Override
    public List<MarketDto> queryAllMarkets() {
        List<Market> list = list(new LambdaQueryWrapper<Market>().eq(Market::getStatus, 1));
        return MarketDtoMapper.INSTANCE.convert2Dto(list);
    }

    @Override
    public List<Market> queryByAreaId(Long id) {
        return null;
    }

    @Override
    public boolean save(Market entity) {
        Long sellCoinId = entity.getSellCoinId();
        Long buyCoinId = entity.getBuyCoinId();
        Map<Long, CoinDto> coinDtoMap = coinFeign.findCoins(Arrays.asList(sellCoinId, buyCoinId));
        if (MapUtils.isEmpty(coinDtoMap) || coinDtoMap.size() != 2) {
            throw new RRException("货币输入错误");
        }
        CoinDto sellDto = coinDtoMap.get(sellCoinId);
        CoinDto buyDto = coinDtoMap.get(buyCoinId);
        entity.setName(sellDto.getName() + "/" + buyDto.getName());
        entity.setTitle(sellDto.getTitle() + "/" + buyDto.getTitle());
        entity.setSymbol(sellDto.getName()+buyDto.getName());
        entity.setImg(buyDto.getImg());
        return super.save(entity);
    }
}
